using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsGame.ScoreComponents
{
    /// <summary>
    /// This game component will add a number next to score each time score is added
    /// </summary>
    public class PointAnimator : Microsoft.Xna.Framework.GameComponent
    {
        //Variables for animating string
        bool addPoints, removePoints;
        bool animatePointModifier, stopAnimate;
        Vector2 scoreModifierPosition;
        float hudColorBlend;
        int addedScoreAmount;
        float fontScale;

        Random rnd;

        PlayerComponent player;

        public PointAnimator(Game game, PlayerComponent player)
            : base(game)
        {
            this.player = player;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            //Set variables start values
            hudColorBlend = 0;
            fontScale = 1;
            stopAnimate = false;

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime, ScoreComponent score, int listIndex, String scoreString, SpriteFont hudFont)
        {
            #region Old animation method. Saving for reference
            /*
            //Animate point modifier notification
            if (animatePointModifier)
            {
                //Move notification downwards
                scoreModifierPosition.Y += 0.02f * gameTime.ElapsedGameTime.Milliseconds;

                //Blend color from transparent to full color
                if (hudColorBlend < 1)
                    hudColorBlend += 0.002f * gameTime.ElapsedGameTime.Milliseconds;


                //If destination is reached, reset booleans
                if (scoreModifierPosition.Y > score.GetScorePosition().Y + 4)
                    stopAnimate = true;


                if (stopAnimate)
                {
                    animatePointModifier = false;
                    addPoints = false;
                    removePoints = false;
                    stopAnimate = false;
                }
            }
            else
                score.RemoveListEntry(listIndex); //Remove this object from list when animation is finished

            //Measure string to determine where to put modifier string
            scoreModifierPosition.X = hudFont.MeasureString(scoreString).X + 10;
            */
            #endregion


            #region Another, fancier, way to animate

            if (animatePointModifier)
            {
                //Blend color from transparent to full color and scale while blending
                if (hudColorBlend < 1)
                {
                    hudColorBlend += 0.002f * gameTime.ElapsedGameTime.Milliseconds;
                    fontScale += 0.003f * gameTime.ElapsedGameTime.Milliseconds;
                }
                else
                    stopAnimate = true;

                if (stopAnimate)
                {
                    animatePointModifier = false;
                    addPoints = false;
                    removePoints = false;
                    stopAnimate = false;
                }
            }
            else
                score.RemoveListEntry(listIndex); //Remove this object from list when animation is finished

            #endregion

            base.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch, SpriteFont hudFont, Camera2DComponent camera)
        {
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, camera.getMatrix());

            //Draw different strings depending on if where adding or removing points
            if (addPoints)
                spriteBatch.DrawString(hudFont, "+" + addedScoreAmount.ToString(), scoreModifierPosition, Color.Multiply(new Color(68, 170, 34), hudColorBlend), 0, new Vector2(10, 10), fontScale, SpriteEffects.None, 0);
            else if (removePoints)
                spriteBatch.DrawString(hudFont, "-" + addedScoreAmount.ToString(), scoreModifierPosition, Color.Multiply(new Color(170, 68, 34), hudColorBlend));
            spriteBatch.End();
        }


        //Set animation and whether we are removing or adding points
        public void Animate(bool addPoints, bool removePoints)
        {
            //Always set animation to true
            animatePointModifier = true;

            this.addPoints = addPoints;
            this.removePoints = removePoints;
        }

        //Check if animating or not
        public bool IsAnimating() { return this.animatePointModifier; }

        //Getters and setters for position
        public Vector2 GetPosition() { return this.scoreModifierPosition; }

        public void SetPosition(Vector2 value) { this.scoreModifierPosition = value; }

        //Setter for setting how much score is added
        public void SetAddedScoreAmount(int value) { this.addedScoreAmount = value; }
    }
}
